/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.server.db;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author czarek
 */
public class DatabaseHyperSql extends Database {
    
    private static final Logger log = Logger.getLogger(DatabaseHyperSql.class.getName());

    @Override
    protected String getConnectionUrl() {
        return "jdbc:hsqldb:"+dbType.toString().toLowerCase()+":" + dbFile;
    }

    @Override
    protected String getUsername() {
        return "sa";
    }

    @Override
    protected String getPassword() {
        return "";
    }
    
    public enum DBType{FILE,MEM};

    private final String dbFile;
    private final DBType dbType;
    
    public DatabaseHyperSql(String dbFile,DBType typ){
        this(dbFile,typ,null);
    }

    public DatabaseHyperSql(String dbFile,DBType typ,Boolean initDb) {
        this.dbFile = dbFile;
        this.dbType = typ;
        
        if((initDb==null&&typ==DBType.MEM)||(initDb!=null&&initDb)){
            initDb();
        }
        polacz();
    }

    public DatabaseHyperSql(String dbFile, String dbType) {
        this(dbFile,dbType,null);
    }
    
    public DatabaseHyperSql(String dbFile, String dbType, Boolean initDb){
        this(dbFile, DBType.valueOf(dbType),initDb);
    }
    
    

    public DatabaseHyperSql() {
        this("run.hdb",DBType.FILE);
        
    }

    @Override
    protected Class<? extends Driver> getDriverClass() throws RuntimeException {
        return org.hsqldb.jdbcDriver.class;
    }

    protected Connection createConnection() throws RuntimeException {
        try {
            return DriverManager.getConnection(getConnectionUrl(), getUsername(), getPassword());
            
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHyperSql.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException("Błąd podczas tworzenia połączenia");
        }

    }
    
    @Override
    protected String getDbInitPath() {
        return "builder_hsql.sql";
    }

    @Override
    public void rozlacz() 
    {
        
        try{
            super.rozlacz();
            Connection con = getDataSource().getConnection();
            Statement stmt = null;
            
            try{
                try {
                    stmt = con.createStatement();
                    stmt.execute("SHUTDOWN;");
                } catch (SQLException ex) {
                    Logger.getLogger(DatabaseHyperSql.class.getName()).log(Level.SEVERE, null, ex);
                }
            }finally{
                try {
                    if(stmt != null)
                        stmt.close();
                    this.releaseConection(con);
                    con.close();                    
                            
                } catch (SQLException ex) {
                    Logger.getLogger(DatabaseHyperSql.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
        } catch (SQLException ex) {
            log.log(Level.SEVERE, null, ex);
        }
        
    }
    
    
    
    
}
